#include "CircularObstacle.h"
#include "CircularObstacleSketcher.h"
#include "GeometryHelper.h"

CircularObstacle::CircularObstacle(void)
{
	this->setType("CircularObstacle");
}

//**************************************************************************************************

float CircularObstacle::getMinXPos(){
	return this->getPosition()->getAxisX()-this->radius;
}

float CircularObstacle::getMinYPos(){	
	return this->getPosition()->getAxisY()-this->radius;
}

float CircularObstacle::getMaxXPos(){	
	return this->getPosition()->getAxisX()+this->radius;
}

float CircularObstacle::getMaxYPos(){
	return this->getPosition()->getAxisY()+this->radius;
}

void CircularObstacle::setPosition(Position * position){
	this->position = position;
	float cx = SketcherHelper::getInstance()->getCoeficiente();
	this->radius = abs(this->radius*cx);
}

Position *CircularObstacle::getPosition(){
	return this->position;
}

Color * CircularObstacle::getElementColor(){
	return this->elementColor;
}

void CircularObstacle::setElementColor(Color * color){
	this->elementColor = color;
}

Texture *CircularObstacle::getTexture(){
	return this->texture;
}

void CircularObstacle::setTexture(Texture *texture){
	this->texture = texture;
}

Color * CircularObstacle::getLineColor()
{
	return this->lineColor;
}

void CircularObstacle::setLineColor( Color * color )
{
	this->lineColor = color;
}

void CircularObstacle::draw()
{
	Sketcher* sketcher = new CircularObstacleSketcher(this);
	sketcher->draw();
	delete(sketcher);
}


CircularObstacle::~CircularObstacle(void)
{
}

void CircularObstacle::setRadius( float radius )
{
	this->radius = radius;
}

float CircularObstacle::getRadius()
{
	return this->radius;
}

void CircularObstacle::evaluateCollision( Ball * ball ){

	Velocity * ballVelocity = ball->getVelocity();
	float vx = ballVelocity->getXComponent();
	float vy = ballVelocity->getYComponent();

	float m = vy / vx;
	Position* nearer=GeometryHelper::getInstance()->getNearerPoint(this->position,ball->getPosition()->getPixelX(),m,ball->getPosition()->getPixelY());
	float distancia= GeometryHelper::getInstance()->calculateDistance(nearer,this->position);
	float maxDistancia=(this->getRadius()+ball->getRadius());

	if(distancia < maxDistancia){
		float distanceAdjust = pow( (pow((float)(this->getRadius()+ball->getRadius()),2)-pow((float)distancia,2)),0.5f );
		float adjustedDistance = ball->calculateDistance(nearer)-distanceAdjust;
		Position * collisionPoint = ball->calculatePosition(adjustedDistance);
		float anguloSalida = GeometryHelper::getInstance()->getAngle(this->getPosition(),collisionPoint);
		Collision * collision = NULL;
		if( ball->hasImpactSense(nearer) ) {
			collision = new Collision();
			collision->setDeltaX(cos(anguloSalida));
			collision->setDeltaY(sin(anguloSalida));
			collision->setPoint(collisionPoint);
			collision->setDistance(adjustedDistance);
			collision->setElement(static_cast<Element*>(this));
			ball->evaluateNextCollision( collision);
		}
	}
}